#!/bin/bash -e
#
# Copyright (c) 2007-2008 The PureDarwin Project.
# All rights reserved.
#
# @LICENSE_HEADER_START@
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# @LICENSE_HEADER_END@
#

#
# probono <probono@puredarwin.org>
# aladin <aladin@puredarwin.org>
#

#
# Basic tool to add users in Darwin 9
# Version 0.200801012 
#

#
# Changelog
#
# 20080321 - See `hg log' (mercurial) from now - aladin
# 20081211 - Initial release - probono

# Default shell
T_SHELL=/bin/bash

# Be root
if [ "$UID" -ne 0 ]
then
	echo "You must be root in order to use $(basename $0)"
	exit 1
fi

# Otherwise manipulating users doesn't work in -s mode
launchctl load /System/Library/LaunchDaemons/com.apple.DirectoryServices.plist 2>/dev/null

# Prompt for username
echo ""
echo -n "Username: "
read T_USERNAME

# Check whether user already exists
EXISTS=0
dscl . -read /Users/${T_USERNAME} >/dev/null 2>&1 && EXISTS=1
if [ "$EXISTS" == "1" ]; then
	echo "User ${T_USERNAME} already exists"
	exit 1
fi

# Prompt for realname
echo -n "Realname: "
read T_REALNAME

# Find next free UID/GID, starting at 1000
typeset -i id=1000
while [[ "$( dscl . search Users UniqueID $id || 
             dscl . search Groups PrimaryGroupID $id )" ]] ; do
    let ++id
done
T_UID_DEFAULT=$id
T_GID_DEFAULT=$id

echo -n "UID (default: ${T_UID_DEFAULT}): "
read T_UID
echo -n "GID (default: ${T_GID_DEFAULT}): "
read T_GID
	
if [ "$T_UID" == "" ]; then
	T_UID=${T_UID_DEFAULT}
fi
	
if [ "$T_GID" == "" ]; then
	T_GID=${T_GID_DEFAULT}
fi

# Prompt for home directory
echo -n "Home directory (default: /Users/${T_USERNAME}): "
read T_HOMEDIR;

if [ "$T_HOMEDIR" == "" ]; then
	T_HOMEDIR="/Users/${T_USERNAME}"
fi

# Check whether home directory already exists
if [ -e ${T_HOMEDIR} ]; then
	echo "${T_HOMEDIR} already exists"
	exit 1
fi

# Prompt for admin group
echo -n "Add ${T_USERNAME} to the admin group? (yes/no): "
read CHOICE;

if [ "$CHOICE" == "yes" ]; then
	ADMIN_USER=1
fi

# Display summary
echo ""
echo "Username: ${T_USERNAME}"
echo "Realname: ${T_REALNAME}"
echo "UID: ${T_UID}"
echo "GID: ${T_GID}"
echo "Shell: ${T_SHELL}"
echo "Home: ${T_HOMEDIR}"
echo ""
echo -n "Add (yes/no): "
read CHOICE

if [ "$CHOICE" != "yes" ]; then
	exit 0
fi

# Create user
dscl . -create /Users/${T_USERNAME}
dscl . -create /Users/${T_USERNAME} UserShell ${T_SHELL}
dscl . -create /Users/${T_USERNAME} RealName "${T_REALNAME}"
#dscl . -create /Users/${T_USERNAME} UniqueID ${T_UID}
dscl . -create /Users/${T_USERNAME} uid ${T_UID}
#dscl . -create /Users/${T_USERNAME} PrimaryGroupID ${T_GID}
dscl . -create /Users/${T_USERNAME} gid ${T_GID}
#dscl . -create /Users/${T_USERNAME} NFSHomeDirectory ${T_HOMEDIR}
dscl . -create /Users/${T_USERNAME} home ${T_HOMEDIR}

# Set password with passwd; otherwise it will not work
passwd ${T_USERNAME}

# Add to admin group
if [ "$ADMIN_USER" == "1" ]; then
	dscl . -append /Groups/admin GroupMembership ${T_USERNAME}
fi

# Create and populate home directory 
mkdir -p ${T_HOMEDIR}
cp -R "/System/Library/User Template/English.lproj/" ${T_HOMEDIR}/
chown ${T_UID}:${T_GID} ${T_HOMEDIR}
echo ""
echo "User ${T_USERNAME} created"
echo ""
